;=========================================================
;
; Variable Length Codes encoded smooth scroller
; (c) 2010 ccr/TNSP^pWp <ccr@tnsp.org>
;
;=========================================================

	scrrow		= 15
	screen 		= $0400
	scroll 		= screen + (40 * scrrow)
	colors 		= $d800 + (40 * scrrow)
	data_len	= data_end - data
	xscroll		= $fb

;=========================================================


	orig = $0326	; load at basic's CHROUT vector

	.word orig
	* = orig

	.word start	; set CHROUT vector to our start address
	.word $f6ed	; appease the cruel gods here

;=========================================================

code1:	jsr get2mbits
	
setchr: tax
	lda codetab,x
	sta scroll+39

;=========================================================

start:	; Initialize, screen colours
	sei
	ldy #0		; we need Y = 0 later

	; Main loop
main:	jsr $ba78	; this idea was stolen from TNT/BF :P
	lda #$bf
raswt1:	cmp $d012
	bne raswt1
	.byte $cf	; dcp $d016
	.word $d016
	bne main

	; Move screen, fetch moar data
	; Y = 0
movelp:	lda scroll+1,y
	sta scroll,y
	lda #7		; make it colourful
	sta colors,y
	iny
	bne movelp
	; Y = 0 again

	sta $d016	; ... :P
	asl
	sta $d021

;=========================================================
; Decompress variable length code encoded text
;=========================================================

	jsr get2mbits
	beq main	; trust that the scrolled in char is space ...

	lsr
	beq code1
	bcc code2

code3:
	jsr get2mbits
	jsr get2bits
	adc #12
	bne setchr

code2:
	jsr get2mbits
	jsr getbit
	adc #4
	bne setchr

;=========================================================
; Fetch 2 bits from datastream, return in A
; also changes flags and X register
;=========================================================
get2mbits:
	tya
get2bits:
	jsr getbit

;=========================================================
; Fetch one bit from the datastream, return it in
; A register, changes X register.
;=========================================================
getbit:
	ldx #data_len
bitlp:	rol data,x
	dex
	bne bitlp
	rol
	rts

;=========================================================
; Decoding table
#include "tables.asm"


; Compressed scroller text data	
data:
	.byte 0
	.bin 0,0,"skrolli.bin"
data_end:
